/*
 * Copyright 2009 by Tran Nhut Trung.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.gdevelop.gwtxp.rebind.gwt;

import com.gdevelop.gwtxp.rebind.AbstractTag;
import com.gdevelop.gwtxp.rebind.Tag;

import com.google.gwt.core.ext.typeinfo.NotFoundException;


public class TableCellTag extends AbstractTag{
  private boolean generateAddUIDone = false;
  public TableCellTag() {
  }

  public Class getUIClass() {
    return null;
  }

  public void generateDeclaration() {
    // No-op
  }

  public void generateAddUI(Tag child) throws NotFoundException {
    if (generateAddUIDone){
      return;
    }
    
    ((HTMLTableTag)parent).generateAddTableCell(this, child);
    
    ((HTMLTableTag)parent).generateSetCellAttibutes(this, child);
    generateAddUIDone = true;
  }
  public void generateAddUI_(Tag child) {
    String row = this.getAttribute("row");
    String col = this.getAttribute("col");
    if ((row == null) || (col == null)){
      throw new RuntimeException("TableCell must have row and col attribute.");
    }
    
    // parent must be HTMLTable (Grid, FlexTable)
    out.println(parent.getVarName() + ".setWidget(" + row + ", " + col + ", " + child.getVarName() + ");");

    if (parent instanceof FlexTableTag){
      String rowSpan = this.getAttribute("rowSpan");
      String colSpan = this.getAttribute("colSpan");
      if (rowSpan != null){
        out.println(parent.getVarName() + ".getFlexCellFormatter().setRowSpan(" + row + ", " + col + ", " + rowSpan + ");");
      }
      if (colSpan != null){
        out.println(parent.getVarName() + ".getFlexCellFormatter().setColSpan(" + row + ", " + col + ", " + colSpan + ");");
      }
    }

    if (parent instanceof HTMLTableTag){
      String height = this.getAttribute("height");
      if (height != null){
        height = "\"" + height + "\"";
        setCellFormatter(row, col, "height", height);
      }
      String width = this.getAttribute("width");
      if (width != null){
        width = "\"" + width + "\"";
        setCellFormatter(row, col, "width", width);
      }

      String horizontalAlignment = this.getAttribute("horizontalAlignment");
      if (horizontalAlignment != null){
        horizontalAlignment = horizontalAlignment.toLowerCase();
        if ("left".equals(horizontalAlignment)){
          horizontalAlignment = "HasHorizontalAlignment.ALIGN_LEFT";
        }else if ("center".equals(horizontalAlignment)){
          horizontalAlignment = "HasHorizontalAlignment.ALIGN_CENTER";
        }else if ("right".equals(horizontalAlignment)){
          horizontalAlignment = "HasHorizontalAlignment.ALIGN_RIGHT";
        }else{
          horizontalAlignment = null;
        }
        if (horizontalAlignment != null){
          setCellFormatter(row, col, "horizontalAlignment", horizontalAlignment);
        }
      }

      String verticalAlignment = this.getAttribute("verticalAlignment");
      if (verticalAlignment != null){
        verticalAlignment = verticalAlignment.toLowerCase();
        if ("top".equals(verticalAlignment)){
          verticalAlignment = "HasVerticalAlignment.ALIGN_TOP";
        }else if ("middle".equals(verticalAlignment)){
          verticalAlignment = "HasVerticalAlignment.ALIGN_MIDDLE";
        }else if ("bottom".equals(verticalAlignment)){
          verticalAlignment = "HasVerticalAlignment.ALIGN_BOTTOM";
        }else{
          verticalAlignment = null;
        }
        if (verticalAlignment != null){
          setCellFormatter(row, col, "verticalAlignment", verticalAlignment);
        }
      }

      String styleName = this.getAttribute("styleName");
      if (styleName != null){
        styleName = "\"" + styleName + "\"";
        setCellFormatter(row, col, "styleName", styleName);
      }
      String stylePrimaryName = this.getAttribute("stylePrimaryName");
      if (stylePrimaryName != null){
        stylePrimaryName = "\"" + stylePrimaryName + "\"";
        setCellFormatter(row, col, "stylePrimaryName", stylePrimaryName);
      }

      String visible = this.getAttribute("visible");
      if (visible != null){
        if (!"true".equals(visible)){
          visible = "false";
        }
        setCellFormatter(row, col, "visible", visible);
      }

      String wordWrap = this.getAttribute("wordWrap");
      if (wordWrap != null){
        if (!"true".equals(wordWrap)){
          wordWrap = "false";
        }
        setCellFormatter(row, col, "wordWrap", wordWrap);
      }
    }
  }
  
  private void setCellFormatter(String row, String col, String propName, String value){
    if (value != null){
      out.println(parent.getVarName() + ".getCellFormatter()." + elContext.getSetMethodName(propName) + "(" + row + ", " + col + ", " + value + ");");
    }
  }
}
